import {portDelete} from "@/api/strategy/portManagement"
import {useCreateModal} from "@/hooks"
import EditModal from './edit-modal.vue'
import {message, Modal} from "ant-design-vue"
import {createVNode} from "vue"
import {QuestionCircleOutlined} from "@ant-design/icons-vue"
import {docCtlDelete} from "@/api/strategy/docControl"
import {checkStatus} from "@/utils/http/axios/checkStatus"

export const columns = [ // 进程策略
    {
        title: '策略名称',
        dataIndex: 'name',
    },
    {
        title: '进程名',
        dataIndex: 'processname',
    },
    {
        title: '协议',
        dataIndex: 'protocol',
        slots: {
            customRender: 'protocol'
        },
        slotsType: 'format',
        slotsFunc: (val) => ({
            0: 'TCP',
            1: 'UDP',
            2: 'TCP,UDP'
        }[val])
    },
    {
        title: '端口',
        dataIndex: 'port',
    },
    {
        title: '是否启用',
        dataIndex: 'using',
        slots: {
            customRender: 'using'
        },
        slotsType: 'format',
        slotsFunc: (val) => ({
            1: '是',
            0: '否'
        }[val])
    },
    {
        title: '操作',
        dataIndex: 'action',
        width: 200,
        slots: {
            customRender: 'action'
        },
        actions: [
            {
                type: 'popconfirm', // 控制类型，默认为a,可选： select | button | text
                key: 'fileid', // 删除的依据，如果需要根据多个字段删除，则字段之间以英文逗号分隔开，例如： id, name
                text: '删除',
                permission: 'port:delete',
                props: {
                  type: 'danger'
                },
                func: ({record}, callback) => {
                    portDelete({ids: [record.id],force:0},{isShowMessage:false}).then(res=>{
                        if(-5==Number(res.code)){
                            Modal.confirm({
                                title: '提示',
                                icon: createVNode(QuestionCircleOutlined),
                                content: '策略已在模板中使用，是否解除所有绑定并删除',
                                onOk: async () => {
                                    await portDelete({ids:[record.id],force:1},{isShowMessage: true})
                                    callback()
                                }
                            })
                        }
                        else{
                            if(0==Number(res.code)){
                                message.success(res.message)
                            }
                            else {
                                checkStatus(res.code,res.message,true)
                            }
                            callback()
                            return Promise.resolve()
                        }
                    })
                },
            },
            {
                type: 'button', // 控制类型，默认为a,可选： select | button | text
                key: 'fileid', // 删除的依据，如果需要根据多个字段删除，则字段之间以英文逗号分隔开，例如： id, name
                text: '编辑',
                permission: 'port:update',
                props: {
                    type: 'warning'
                },
                func: ({record}, callback) => useCreateModal(EditModal, {
                    fields: record,
                    callback
                }),
            }
        ]
    },
]
